package xin.marcher.module.diplomacy.manager.custom.db;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Sets;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import xin.marcher.module.diplomacy.domain.entity.SkuPriceSellerRelationDO;
import xin.marcher.module.diplomacy.manager.common.cache.RedisStringCache;
import xin.marcher.module.diplomacy.manager.common.db.AbstractRedisStringDatabase;
import xin.marcher.module.diplomacy.mapper.SkuPriceSellerRelationMapper;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

/**
 * 卖家商品价格
 */
@Slf4j
@Service("skuPriceSellerStringDatabase")
public class SkuPriceSellerStringDatabase extends AbstractRedisStringDatabase<SkuPriceSellerRelationDO> {

    @Resource
    private SkuPriceSellerRelationMapper skuPriceSellerRelationMapper;

    /**
     * 表名
     */
    private static final String TABLE_NAME = "sku_price_seller_relation";

    /**
     * 卖家类型
     */
    public static final String SELLER_TYPE = "sellerType";

    /**
     * 卖家ID
     */
    public static final String SELLER_ID = "sellerId";

    /**
     * 商品ID
     */
    public static final String SKU_ID = "skuId";

    /**
     * 数据源单表
     *
     * @return java.lang.String
     */
    @Override
    public String getTableName() {
        return TABLE_NAME;
    }

    /**
     * 数据源全量表
     *
     * @param
     * @return java.util.Set<java.lang.String>
     */
    @Override
    public Set<String> getTableNameSet() {
        return Sets.newHashSet(TABLE_NAME);
    }

    /**
     * 获取表数据
     *
     * @param tableFieldsMap key 数据库字段，value 字段值
     * @param queryType      查询类型，多数据查询时区分
     * @return java.util.Optional<T>
     */
    @Override
    public Optional<SkuPriceSellerRelationDO> getTableData(Map<String, Object> tableFieldsMap, String queryType) {
        return Optional.empty();
    }

    /**
     * 获取表多数据
     *
     * @param tableFieldsMap
     * @param queryType
     * @return java.util.Optional<java.util.List < xin.marcher.module.diplomacy.domain.entity.SkuPriceSellerRelationDO>>
     */
    @Override
    public Optional<List<SkuPriceSellerRelationDO>> listTableData(Map<String, Object> tableFieldsMap, String queryType) {
        if (!tableFieldsMap.containsKey(SELLER_TYPE) || !tableFieldsMap.containsKey(SELLER_ID) || !tableFieldsMap.containsKey(SKU_ID)) {
            throw new IllegalArgumentException();
        }

        // 卖家商品价格信息
        Integer sellerType = (Integer) tableFieldsMap.get(SELLER_TYPE);
        String sellerId = (String) tableFieldsMap.get(SELLER_ID);
        String skuId = (String) tableFieldsMap.get(SKU_ID);
        skuId = skuId.substring(0, 10);
        QueryWrapper<SkuPriceSellerRelationDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("SELLER_TYPE", sellerType);
        queryWrapper.eq("SELLER_ID", sellerId);
        queryWrapper.eq("SKU_ID", skuId);

        List<SkuPriceSellerRelationDO> skuPriceSellerRelationDOList = skuPriceSellerRelationMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(skuPriceSellerRelationDOList)) {
            return Optional.empty();
        }
        return Optional.of(skuPriceSellerRelationDOList);
    }

    /**
     * 根据ID批量获取数据
     *
     * @param idSet     表主键ID集合
     * @param queryType 查询类型，多数据查询时区分
     * @return java.util.Optional<xin.marcher.module.diplomacy.manager.common.source.RedisStringSource < T>>
     */
    @Override
    public Optional<RedisStringCache<SkuPriceSellerRelationDO>> listTableDataByIdSet(Set<Long> idSet, String queryType) {
        return Optional.empty();
    }

    @Override
    public Optional<RedisStringCache<SkuPriceSellerRelationDO>> listTableDataByIdSet(List<Long> idSet, String queryType) {
        return Optional.empty();
    }

}
